package com.hm.controller;

import com.hm.pojo.SearchItem;
import com.hm.serivce.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Administrator
 * @title: SearchController
 * @projectName Hema
 * @description: TODO
 * @date 2019/5/1617:14
 */
@RestController
@Api(value = "搜索引擎接口",tags = "搜索引擎接口")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @RequestMapping(value = "search",method = RequestMethod.GET)
    @ApiOperation(value = "搜索引擎接口")
    public List<SearchItem> search(String keyword, Model model) throws Exception{
        /*keyword = new String(keyword.getBytes("iso8859-1"), "utf-8");*/
        List<SearchItem> itemList = searchService.search(keyword);
        return itemList;
    }

    //采用redis的list实现“搜索历史”
    @RequestMapping(value = "search/history",method = RequestMethod.POST)
    @ApiOperation(value = "搜索历史记录接口")
    public List<String> getAutoMatchs(Integer userId, String pre){
        searchService.updateList(userId, pre);
        List<String> historySearch = searchService.getAutoMatchs(userId, pre);
        return historySearch;
    }

    //采用redis的list实现“自动匹配”
    @RequestMapping(value = "search/automatching",method = RequestMethod.POST)
    @ApiOperation(value = "搜索自动匹配接口")
    public List<String> getDefaultAutoMatchs(String pre){
        return searchService.getDefaultAutoMatchs(pre);
    }
}
